define [
	'jquery'
	'sammy/plugins/sammy.template-0.7.1.min.js'
	'jquery.couch.js'
	'helper'
], ($, undef, undef, H) ->
	S = {}
	$.fn.mb_view_receipt = (SamCtx) ->
			ins = @
			$this = $(@)
			S = SamCtx
			$tpl = $('.tpl', @).hide()
									
			unless @mb_plugin()
				@mb_plugin "view_receipt"
				$.subscribe "mb.donation", (e, arg) ->
					_dnID = arg['id']
					return if arg['for'] isnt 'receipt'
					$.couch.db("vcn-members").openDoc _dnID, {
						success: (donation) ->
							donation = H.objFormat(donation)
							$.couch.db("vcn-members").openDoc donation["donor_id"], {
								success: (donor) ->
									donor = H.objFormat(donor)
									d = (new Date()).toDateString().split(' ')
									donor._date = "#{d[2]}-#{d[1]}-#{d[3].slice(2)}"
									
									# Redefining donation_date 
									pd = donation.donation_date.split(' ')[0].split('-')
									nd = new Date()
									nd.setUTCFullYear(pd[0])
									nd.setUTCMonth(Number(pd[1]) - 1)
									nd.setUTCDate(pd[2])
									nd = nd.toDateString().split(' ')
									donor._ndate = "#{nd[2]}-#{nd[1]}-#{nd[3].slice(2)}"

									# Determine whether the received should be the organizaiton name
									# or individual name.
									if donor.org_name?
										donor.received = donor.org_name
									else 
										donor.received = "#{donor.last_name}, #{donor.first_name}"

									donor.province = ", #{donor.province}" if donor.province isnt ""
									donor.donation_type = donation.donation_type
									donor.donation_amount = donation.donation_amount

									$('.content', ins).empty().append(S.content $tpl, donor)


									$('td:not(.label)')
										.css('padding-left', '5px')
								

								}
					
						}
		return @
	return
